package com.gxa.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.dto.MyParam;
import com.gxa.dto.ResultDto;
import com.gxa.exception.SystemException;
import com.gxa.pojo.Role;
import com.gxa.pojo.User;
import com.gxa.service.RoleService;
import com.gxa.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/list")
    private String roleListPage() {
        return "role/list";
    }

    /**
     * 按条件查询所有角色
     * @param role
     * @param myParam
     * @return
     */
    @PostMapping("/findRole")
    @ResponseBody
    private ResultDto findRoleByParam(Role role, MyParam myParam) {
        List<Role> roleByParam;
        if (myParam == null || myParam.getPage() == null || myParam.getLimit() == null) {
            // 查询所有角色
            roleByParam = roleService.findRoleByParam(role);
            return Response.success(200, "查询成功", roleByParam);
        } else {
            // 分页查询角色
            PageHelper.startPage(myParam.getPage(), myParam.getLimit());
            roleByParam = roleService.findRoleByParam(role);
            PageInfo<Role> pageInfo = new PageInfo<>(roleByParam);
            return Response.success(200, "查询成功", roleByParam, Integer.valueOf(pageInfo.getTotal()+""));
        }
    }

     /**
     *根据用户id获取用户角色
     * @param userId
     * @return
     */
    @PostMapping("/findRoleById")
    @ResponseBody
    public ResultDto findRoleById(Integer userId){
        if (userId!=null){
            List<Role> roleByUserId = roleService.findRoleByUserId(userId);
            return Response.success("查找成功！", roleByUserId);
        }else {
            return Response.error(1001, "数据参数有误！");
        }


    }


}
